/**
 * 声乐控制类
 */
package com.key;

import java.util.HashMap;

import com.key.util.GameParams;
import com.key.util.UserSystemData;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

@SuppressLint("UseSparseArrays")
public class KeyChineseChessAudio {

	private int iStreamVolume = 0;//声音大小
	private SoundPool soundpool = null;//音效池
	private HashMap<Integer, Integer> soundPoolMap = null;//音效池Map
	private AudioManager mgr = null;//音效控制类

	private MediaPlayer mMediaPlayer = null;//MediaPlayer类

	/**
	 * 初始化MediaPlayer
	 * @param mContext
	 * @param ID
	 */
	public void initMedia(Context mContext, int ID) {
		mMediaPlayer = MediaPlayer.create(mContext, ID);
	}

	/**
	 * 音乐是否循环
	 */
	public void setMediaLoop() {
		mMediaPlayer.setLooping(true);
	}

	/**
	 * 播放音乐
	 */
	public void playMedia() {
		if (!mMediaPlayer.isPlaying()) {
			mMediaPlayer.start();
		}
	}

	/**
	 * 音效控制类初始化
	 * @param mContext
	 * @param iNum 音效数
	 */
	public void init(Context mContext, int iNum) {
		soundpool = new SoundPool(iNum, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();

		mgr = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
		iStreamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
	}

	/**
	 * 取得声音大小
	 * @return
	 */
	public int getStreamVolume() {
		return iStreamVolume;
	}

	/**
	 * 加载音效
	 * @param mContext
	 * @param raw 音效资源id
	 * @param ID 音效id
	 */
	public void loadSfx(Context mContext, int raw, int ID) {
		soundPoolMap.put(ID, soundpool.load(mContext, raw, ID));
	}

	/**
	 * 加载音效
	 * @param mContext
	 * @param id 音效资源id
	 * @return 音效id
	 */
	public int loadRaw(Context mContext, int id) {
		return soundpool.load(mContext, id, 0);
	}

	public void playRaw(int soundID) {
		if (UserSystemData.keyMusic.getSingleChecked()) {
			soundpool.play(soundID,
					(float) UserSystemData.keyMusic.getSingleDegree() / 10,
					(float) UserSystemData.keyMusic.getSingleDegree() / 10, 0,
					0, 1);
		}
	}

	/**
	 * 调整音乐音量
	 */
	public void adjustVolume() {
		if (UserSystemData.keyMusic.getMusicChecked()) {
			mMediaPlayer.setVolume(
					(float) UserSystemData.keyMusic.getMusicDegree() / 10,
					(float) UserSystemData.keyMusic.getMusicDegree() / 10);
		}
	}

	/**
	 * 设置是否静音
	 * @param isMute
	 */
	public void setMusicMute(boolean isMute) {
		// GameParams.musicVolumn = UserSystemData.keyMusic.getMusicDegree();
		if (UserSystemData.keyMusic.getMusicChecked()) {
			adjustVolume();
		} else {
			mMediaPlayer.setVolume(0, 0);
		}
	}
}
